package com.whzykj.common.alipay;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;

import com.whzykj.alipay.config.AlipayConfig;
import com.whzykj.alipay.sign.RSA;
import com.whzykj.alipay.util.AlipayCore;
import com.whzykj.alipay.util.UtilDate;

public class AlipayUtil {

	/******
	 * 支付宝订单支付
	 * @param order_id
	 * @param balance_pay
	 * @param nonce_str
	 * @param ip
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public static String unifiedorderfororder(String order_id,BigDecimal pay_amounts,BigDecimal balance_pays,String user_id) throws UnsupportedEncodingException{
		
		Map<String, String> params=new HashMap<String,String>();
		params.put("app_id", AliPayConst.appid);
		params.put("method", AliPayConst.alipay_method);
		params.put("format", "json");
		params.put("charset", "utf-8");
		params.put("sign_type", "RSA");
		params.put("timestamp", UtilDate.getDateFormatter());
		params.put("version", "1.0");
		params.put("notify_url", AliPayConst.pay_notify_url);
		params.put("biz_content", AlipayUtil.bizcontentfororder(order_id,pay_amounts,balance_pays,user_id));
		String link = AlipayCore.createLinkString(params);
		String sign = RSA.sign(link, AlipayConfig.private_key, "utf-8");
		params.put("sign", sign);
		
		Map<String, String> pm=new HashMap<String,String>();
		pm.put("app_id", URLEncoder.encode(AliPayConst.appid,"utf-8"));
		pm.put("method", URLEncoder.encode(AliPayConst.alipay_method,"utf-8"));
		pm.put("format",  URLEncoder.encode("json","utf-8"));
		pm.put("charset", URLEncoder.encode("utf-8","utf-8"));
		pm.put("sign_type", URLEncoder.encode("RSA","utf-8"));
		//pm.put("sign", URLEncoder.encode(sign,"utf-8"));
		pm.put("timestamp", URLEncoder.encode(UtilDate.getDateFormatter(),"utf-8"));
		pm.put("version", URLEncoder.encode("1.0","utf-8"));
		pm.put("notify_url", URLEncoder.encode(AliPayConst.pay_notify_url,"utf-8"));
		pm.put("biz_content", URLEncoder.encode(AlipayUtil.bizcontentfororder(order_id,pay_amounts,balance_pays,user_id),"utf-8"));
		String signlinks = URLEncoder.encode(sign,"utf-8");
		String links = AlipayCore.createLinkString(pm)+"&sign="+signlinks;
		
		return links;
	}
	
	/****
	 * 支付宝订单支付拼接核心参数
	 * @param out_trade_no
	 * @param balance_pay
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public static String bizcontentfororder(String out_trade_no,BigDecimal pay_amounts,BigDecimal balance_pay, String user_id)throws UnsupportedEncodingException {
		JSONObject obj = new JSONObject();
		obj.put("subject", "百菜园订单支付");
		obj.put("out_trade_no", out_trade_no);
		obj.put("total_amount", pay_amounts.toString());
		obj.put("product_code", "QUICK_MSECURITY_PAY");
		String passback_params = user_id + "|" + balance_pay;
		obj.put("passback_params", URLEncoder.encode(passback_params, "utf-8"));
		return obj.toString();
	}
	
	/******
	 * 支付宝余额充值
	 * @param order_id
	 * @param balance_pay
	 * @param nonce_str
	 * @param ip
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public static String unifiedorder(String order_id,BigDecimal balance_pay) throws UnsupportedEncodingException{
		
		Map<String, String> params=new HashMap<String,String>();
		params.put("app_id", AliPayConst.appid);
		params.put("method", AliPayConst.alipay_method);
		params.put("format", "json");
		params.put("charset", "utf-8");
		params.put("sign_type", "RSA");
		params.put("timestamp", UtilDate.getDateFormatter());
		params.put("version", "1.0");
		params.put("notify_url", AliPayConst.balancepay_notify_url);
		params.put("biz_content", AlipayUtil.bizcontent(order_id,balance_pay));
		String link = AlipayCore.createLinkString(params);
		String sign = RSA.sign(link, AlipayConfig.private_key, "utf-8");
		params.put("sign", sign);
		
		Map<String, String> pm=new HashMap<String,String>();
		pm.put("app_id", URLEncoder.encode(AliPayConst.appid,"utf-8"));
		pm.put("method", URLEncoder.encode(AliPayConst.alipay_method,"utf-8"));
		pm.put("format",  URLEncoder.encode("json","utf-8"));
		pm.put("charset", URLEncoder.encode("utf-8","utf-8"));
		pm.put("sign_type", URLEncoder.encode("RSA","utf-8"));
		//pm.put("sign", URLEncoder.encode(sign,"utf-8"));
		pm.put("timestamp", URLEncoder.encode(UtilDate.getDateFormatter(),"utf-8"));
		pm.put("version", URLEncoder.encode("1.0","utf-8"));
		pm.put("notify_url", URLEncoder.encode(AliPayConst.balancepay_notify_url,"utf-8"));
		pm.put("biz_content", URLEncoder.encode(AlipayUtil.bizcontent(order_id,balance_pay),"utf-8"));
		String signlinks = URLEncoder.encode(sign,"utf-8");
		String links = AlipayCore.createLinkString(pm)+"&sign="+signlinks;
		
		return links;
	}
	
	/****
	 * 拼接核心参数
	 * @param out_trade_no
	 * @param balance_pay
	 * @return
	 */
	public static String bizcontent(String out_trade_no,BigDecimal balance_pay){
		
		JSONObject obj= new JSONObject();
		obj.put("subject", "百菜园余额充值");
		obj.put("out_trade_no", out_trade_no);
		obj.put("total_amount", balance_pay.toString());
		obj.put("product_code", "QUICK_MSECURITY_PAY");
		return obj.toString();
	}
	
	public static void main(String args[]) throws UnsupportedEncodingException
	{
		String order_id="111111111111";
		BigDecimal balance_pay=new BigDecimal("9.00");
		System.out.println(AlipayUtil.unifiedorder(order_id, balance_pay));
	}
	
}
